<?php
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Components\Tools;
use App\Components\DyTools;
use App\Models\DyModels\Infosync;
use Log;
class HomeController extends Controller{
	private $params;
	public function __construct(Request $request) {
		$this->params = $request->all();
	}
	
	
	public function index(){
		$model=new Infosync();
		$data=[
			'countPt'=>0,
			'countPu'=>0,
			'countUser'=>0,
			'countTal'=>0,
			'countTalD'=>0,
			'countVideoDay'=>0,
			'countCommentDay'=>0,
			'countInfoCommentDay'=>0,
			'countUserInfo'=>0,
			'countUserInfoTwo'=>0,
			'countStayVideo'=>0,
			'countTooVideo'=>0,
			'countPrice'=>0,
			'countPriceMeal'=>0,
			'orderList'=>'',
		];
		$data['countPt']=$model->countPt();
		$data['countPu']=$model->countPu();
		$data['countUser']=$model->countUser();
		$data['countTal']=$model->countTal();
		$data['countTalD']=$model->countTalD();
		$data['countVideoDay']=$model->countVideoDay();
		$data['countCommentDay']=$model->countCommentDay();
		$data['countInfoCommentDay']=$model->countInfoCommentDay();
		$data['countUserInfo']=$model->countUserInfo();
		$data['countUserInfoTwo']=$model->countUserInfoTwo();
		$data['countStayVideo']=$model->countStayVideo();
		$data['countTooVideo']=$model->countTooVideo();
		$data['countPrice']=$model->countPrice();
		$data['countPriceMeal']=$model->countPriceMeal();
		$data['orderList']=$model->orderList();
		
		return view('home/index',["data"=>$data]);
	}
  
	/**
	* 抖音授权
	*/
	public function dyAuth(){
		$DyTools=new DyTools();
		$dyClientKey = $DyTools->third('client_key');
		$dyClientSecret = $DyTools->third('client_secret');
		$redirectUri = $DyTools->third('redirect_uri');
		if(isset($this->params['code']) && !empty($this->params['code'])){
			$get_access_token_url = "https://open.douyin.com/oauth/access_token?client_key={$dyClientKey}&client_secret={$dyClientSecret}&grant_type=authorization_code&code={$this->params['code']}";
			$access_token_info = $DyTools->getJson($get_access_token_url);
			Log::info('access_token_info='.json_encode($access_token_info));
			if($access_token_info['data']['error_code'] == 0){
				$oDyAccessToken = new \App\Models\UserModels\DyAccessToken();
				$oDyAccessToken->dataSave($access_token_info,$this->params['state']);
				return redirect('/');
			}else{
				echo $access_token_info['data']['description'];exit;
			}
		}else{
			$redirect_uri = $redirectUri."/douyin/auth";
			$redirect_uri = urlencode($redirect_uri);
			// $scope = 'video.create,video.delete,video.data,video.list,im.share,video.search,video.search.comment,aweme.share,user_info,'
            // . 'following.list,fans.list,fans.check,renew_refresh_token,mobile_alert,mobile,video.comment,enterprise.data,enterprise.im,'
            // . 'item.comment,data.external.user,data.external.item,fans.data,star_top_score_display,star_tops,star_author_score_display,discovery.ent,'
            // . 'data.external.sdk_share,hotsearch,data.external.billboard_game,data.external.billboard_drama,data.external.billboard_car,data.external.billboard_amusement,'
            // . 'data.external.billboard_cospa,data.external.billboard_food,data.external.billboard_travel,data.external.billboard_stars,data.external.billboard_sport,'
            // . 'data.external.fans_favourite,data.external.fans_source,poi.search,incremental_authorization';
			$scope = 'video.create,video.delete,video.data,video.list,im.share,video.search,video.search.comment,aweme.share,user_info,'
            . 'following.list,fans.list,fans.check,renew_refresh_token,mobile_alert,mobile,video.comment,enterprise.data,enterprise.im,'
            . 'item.comment,data.external.user,data.external.item,fans.data,star_top_score_display,star_tops,star_author_score_display,'
            . 'data.external.sdk_share,'
            . 'data.external.fans_favourite,poi.search,micapp.is_legal,incremental_authorization';
			$url = "https://open.douyin.com/platform/oauth/connect/?client_key={$dyClientKey}&response_type=code&scope={$scope}&redirect_uri={$redirect_uri}&state={$this->params['state']}";
			header("Location:".$url);
		}
	}
  
	public function dyConfig(){
		Log::info('dyConfig='.json_encode($this->params));
		echo json_encode($this->params['content']);
	}

	public function qiniuNotify(){
		$params = file_get_contents('php://input', 'r');
		Tools::commandsLog('params='.$params);
		$data = json_decode($params,true);
		if(is_array($data)){
			$inputKey	= $data['inputKey'];
			$outputKey = $data['items'][0]['key'];
			$oVideo = new \App\Models\CourseModels\Video();
			$videoData = $oVideo->db_get(['video_url'=>['=',$inputKey],'status'=>['>=',0]]);
			if(!empty($videoData)){ //课程
				$oVideo->db_update('',$videoData['dried_food_video_id'],['deal_video_url'=>$outputKey,'deal_status'=>3]);
			}else{
				//直播回放
				$oLiveCourse = new \App\Models\CourseModels\LiveCourse();
				$lcData = $oLiveCourse->db_get(['video_url'=>['=',$inputKey]]);
				if(!empty($lcData)){
					$oLiveCourse->db_update('',$lcData['id'],['deal_video_url'=>$outputKey,'deal_status'=>3]);
				}
			}
		}
		echo 'success';exit;
	}
}
